<?php
/**
 * Powered By ThinkPHP 5.0.12
 * Created by PhpStorm.
 * Date: 2018/12/25
 * Author: Zhangzhenbei <zhenbeigg@163.com>
 */

namespace app\api\controller;

use think\Db;
class Cart extends Common
{
    /**
    *获取用户购物车商品列表
    *@access public 
    *@param int $user_id 用户id
    */
    public function get_user_cart_list()
    {
        $user_id = input('param.user_id');
        if(!$user_id){
            json_msg(1,'请求错误');
        }
        $list = Db::name('cart')->where('user_id',$user_id)->field('id,goods_id,goods_count,goods_sell_price,goods_img_url,goods_title')->select();
        $list = edit_img_url($list,'goods_img_url');
        foreach($list as $k=>$v){
            $list[$k]['selected'] = 'true';
        }
        if($list){
            json_msg_data(0,'ok',$list);
        }else{
            json_msg(1,'暂无数据'); 
        }
    }
    /**
    *添加购物车
    *@access public 
    *@param int $user_id 用户id
    *@param int $goods_id 商品id
    *@param string $goods_count 商品数量
    */
    public function add_cart()
    {
        $userId = input('param.user_id');
        $goodsId = input('param.goods_id');
        $goodsCount = input('param.goods_count');
        if(!$userId || !$goodsId ||!$goodsCount){
            json_msg(1,'请求错误'); 
        }
        if ($goodsCart = Db::name('cart')->where(['user_id'=>$userId,'goods_id'=>$goodsId])->find())
        {
            $result = Db::name('cart')->where(['user_id'=>$userId,'goods_id'=>$goodsId])->setField('goods_count',$goodsCart['goods_count']+$goodsCount);
        }else {
            $goods = Db::name('goods')->field('title,sell_price,img_url')->where('id',$goodsId)->find();
            $data = [
                'user_id'=>$userId,
                'goods_id'=>$goodsId,
                'goods_count'=>$goodsCount,
                'goods_title'=>$goods['title'],
                'goods_sell_price'=>$goods['sell_price'],
                'goods_img_url' =>$goods['img_url']
            ];
            $result = Db::name('cart')->insert($data);
        }
        if ($result)
        {
            $res['count'] = $goodsCart = Db::name('cart')->where(['user_id'=>$userId])->sum('goods_count');
            json_msg_data(0,'成功加入购物车',$res);
        } else {
            json_msg(1,'加入失败');
        }
    }
    /**
    *删除购物车
    *@access public 
    *@param int $cartId 购物车id
    */
    public function del_cart()
    {
        $cartId = input('param.cartId');
        if(!$cartId){
            json_msg(1,'请求错误'); 
        }
        if (Db::name('cart')->delete($cartId)) {
            json_msg(0, '删除成功');
        } else {
            json_msg(1, '删除失败');
        }
    }

    /**
    * 购物车商品数量加减
    *@access public 
    *@param int $cartId 购物车id
    *@param string $action 类型 jia加 jian减
    */
    public function edit_cart_count()
    {
        $cartId = input('param.cartId');
        $action = input('param.action');
        if(!$cartId || !$action){
            json_msg(1,'请求错误'); 
        }
        switch ($action) {
            case 'jia':
                $model = Db::name('cart');
                $cart = $model->field('goods_count,goods_id')->find($cartId);
                $goodsCount = Db::name('goods')->where('id', $cart['goods_id'])->value('stock');
                if ($goodsCount <= $cart['goods_count']) {
                    json_msg(1,'商品库存不足');
                }
                $result = $model->where('id', $cartId)->setInc('goods_count');
                break;
            case 'jian':
                $goodsCount = Db::name('cart')->where('id', $cartId)->value('goods_count');
                if ($goodsCount == 1) {
                    $cart = Db::name('cart')->where('id', $cartId)->find();
                    $data = [
                        'goods_price_sum' => $cart['goods_sell_price'] * $cart['goods_count'],
                        'goods_count'=>$cart['goods_count']
                    ];
                    json_msg_data(0,'操作成功',$data);
                } else {
                    $result = Db::name('cart')->where('id', $cartId)->setDec('goods_count');
                }
                break;
        }
        if ($result) {
            $cart = Db::name('cart')->where('id', $cartId)->find();
            $data = [
                'goods_price_sum' => $cart['goods_sell_price'] * $cart['goods_count'],
                'goods_count'=>$cart['goods_count']
            ];
            json_msg_data(0,'操作成功',$data);
        } else {
            json_msg(1,'系统错误,操作失败');
        }
    }
}